import { Card, Checkbox, Form } from "antd";
import { InnerBanditConfig } from "./config";
import { RuleList } from "./rule";
import ArrayInput from "./ArrayInput";
import { min_app_shell } from '@linksaas-minapp/api';
import { useEffect, useState } from "react";

interface RulePanelProps {
    cfg: InnerBanditConfig;
    onChange: (cfg: InnerBanditConfig) => void;
}

export default function RulePanel(props: RulePanelProps) {
    const [showShellInjection, setShowShellInjection] = useState(false);

    useEffect(() => {
        setShowShellInjection(false);
        if (props.cfg.tests == undefined) {
            return;
        }
        for (const ruleId of ["B602", "B603", "B604", "B605", "B606", "B607", "B609"]) {
            if (props.cfg.tests.includes(ruleId)) {
                setShowShellInjection(true);
                break;
            }
        }
    }, [props.cfg.tests]);

    return (
        <>
            {(props.cfg.tests ?? []).includes("B101") && (
                <>
                    {RuleList.filter(rule => rule.id == "B101").map(rule => (
                        <Card key={rule.id} bordered={false} title={
                            <span>{rule.id} {rule.name}&nbsp;<a onClick={() => min_app_shell.openBrowser(rule.url)}>规则描述</a></span>
                        }>
                            <p>{rule.titleCn}</p>
                            <p>{rule.titleEn}</p>
                            <p>{rule.descCn}</p>
                            <p>{rule.descEn}</p>
                            <Form>
                                <Form.Item label="参数skips">
                                    <ArrayInput valueList={props.cfg.assert_used?.skips ?? ["*_test.py", "*test_*.py"]}
                                        onChange={valueList => {
                                            props.onChange({ ...props.cfg, assert_used: { skips: valueList } })
                                        }} />
                                </Form.Item>
                            </Form>
                        </Card>
                    ))}


                </>
            )}

            {(props.cfg.tests ?? []).includes("B108") && (
                <>
                    {RuleList.filter(rule => rule.id == "B108").map(rule => (
                        <Card key={rule.id} bordered={false} title={
                            <span>{rule.id} {rule.name}&nbsp;<a onClick={() => min_app_shell.openBrowser(rule.url)}>规则描述</a></span>
                        }>
                            <p>{rule.titleCn}</p>
                            <p>{rule.titleEn}</p>
                            <p>{rule.descCn}</p>
                            <p>{rule.descEn}</p>
                            <Form>
                                <Form.Item label="参数tmp_dirs">
                                    <ArrayInput valueList={props.cfg.hardcoded_tmp_directory?.tmp_dirs ?? ["/tmp", "/var/tmp", "/dev/shm"]}
                                        onChange={valueList => {
                                            props.onChange({ ...props.cfg, hardcoded_tmp_directory: { tmp_dirs: valueList } })
                                        }} />
                                </Form.Item>
                            </Form>
                        </Card>
                    ))}
                </>
            )}

            {(props.cfg.tests ?? []).includes("B109") && (
                <>
                    {RuleList.filter(rule => rule.id == "B109").map(rule => (
                        <Card key={rule.id} bordered={false} title={
                            <span>{rule.id} {rule.name}&nbsp;<a onClick={() => min_app_shell.openBrowser(rule.url)}>规则描述</a></span>
                        }>
                            <p>{rule.titleCn}</p>
                            <p>{rule.titleEn}</p>
                            <p>{rule.descCn}</p>
                            <p>{rule.descEn}</p>
                            <Form>
                                <Form.Item label="参数function_names">
                                    <ArrayInput valueList={props.cfg.password_config_option_not_marked_secret?.function_names ?? []}
                                        onChange={valueList => {
                                            props.onChange({ ...props.cfg, password_config_option_not_marked_secret: { function_names: valueList } })
                                        }} />
                                </Form.Item>
                            </Form>
                        </Card>
                    ))}
                </>
            )}

            {(props.cfg.tests ?? []).includes("B110") && (
                <>
                    {RuleList.filter(rule => rule.id == "B110").map(rule => (
                        <Card key={rule.id} bordered={false} title={
                            <span>{rule.id} {rule.name}&nbsp;<a onClick={() => min_app_shell.openBrowser(rule.url)}>规则描述</a></span>
                        }>
                            <p>{rule.titleCn}</p>
                            <p>{rule.titleEn}</p>
                            <p>{rule.descCn}</p>
                            <p>{rule.descEn}</p>
                            <Form>
                                <Form.Item label="参数check_typed_exception">
                                    <Checkbox checked={props.cfg.try_except_pass?.check_typed_exception ?? true} onChange={e => {
                                        e.stopPropagation();
                                        props.onChange({ ...props.cfg, try_except_pass: { check_typed_exception: e.target.checked } })
                                    }} />
                                </Form.Item>
                            </Form>
                        </Card>
                    ))}
                </>
            )}

            {(props.cfg.tests ?? []).includes("B111") && (
                <>
                    {RuleList.filter(rule => rule.id == "B111").map(rule => (
                        <Card key={rule.id} bordered={false} title={
                            <span>{rule.id} {rule.name}&nbsp;<a onClick={() => min_app_shell.openBrowser(rule.url)}>规则描述</a></span>
                        }>
                            <p>{rule.titleCn}</p>
                            <p>{rule.titleEn}</p>
                            <p>{rule.descCn}</p>
                            <p>{rule.descEn}</p>
                            <Form>
                                <Form.Item label="参数function_names">
                                    <ArrayInput valueList={props.cfg.execute_with_run_as_root_equals_true?.function_names ?? []}
                                        onChange={valueList => {
                                            props.onChange({ ...props.cfg, execute_with_run_as_root_equals_true: { function_names: valueList } })
                                        }} />
                                </Form.Item>
                            </Form>
                        </Card>
                    ))}
                </>
            )}

            {(props.cfg.tests ?? []).includes("B112") && (
                <>
                    {RuleList.filter(rule => rule.id == "B112").map(rule => (
                        <Card key={rule.id} bordered={false} title={
                            <span>{rule.id} {rule.name}&nbsp;<a onClick={() => min_app_shell.openBrowser(rule.url)}>规则描述</a></span>
                        }>
                            <p>{rule.titleCn}</p>
                            <p>{rule.titleEn}</p>
                            <p>{rule.descCn}</p>
                            <p>{rule.descEn}</p>
                            <Form>
                                <Form.Item label="参数check_typed_exception">
                                    <Checkbox checked={props.cfg.try_except_continue?.check_typed_exception ?? true} onChange={e => {
                                        e.stopPropagation();
                                        props.onChange({ ...props.cfg, try_except_continue: { check_typed_exception: e.target.checked } })
                                    }} />
                                </Form.Item>
                            </Form>
                        </Card>
                    ))}
                </>
            )}

            {(props.cfg.tests ?? []).includes("B502") && (
                <>
                    {RuleList.filter(rule => rule.id == "B502").map(rule => (
                        <Card key={rule.id} bordered={false} title={
                            <span>{rule.id} {rule.name}&nbsp;<a onClick={() => min_app_shell.openBrowser(rule.url)}>规则描述</a></span>
                        }>
                            <p>{rule.titleCn}</p>
                            <p>{rule.titleEn}</p>
                            <p>{rule.descCn}</p>
                            <p>{rule.descEn}</p>
                            <Form>
                                <Form.Item label="参数bad_protocol_versions">
                                    <ArrayInput valueList={props.cfg.ssl_with_bad_version?.bad_protocol_versions ?? ["PROTOCOL_SSLv2",
                                        "SSLv2_METHOD", "SSLv23_METHOD", "PROTOCOL_SSLv3", "PROTOCOL_TLSv1", "SSLv3_METHOD", "TLSv1_METHOD"]}
                                        onChange={valueList => {
                                            props.onChange({ ...props.cfg, ssl_with_bad_version: { bad_protocol_versions: valueList } })
                                        }} />
                                </Form.Item>
                            </Form>
                        </Card>
                    ))}
                </>
            )}
            {showShellInjection == true && (
                <Card title="ShellInjection配置" bordered={false}>
                    <p>当打开B602,B603,B604,B605,B606,B607,B609中任意一条规则后，都会出现此配置。</p>
                    <Form labelCol={{ span: 3 }}>
                        <Form.Item label="参数subprocess">
                            <ArrayInput valueList={props.cfg.shell_injection?.subprocess ?? []}
                                onChange={valueList => {
                                    const oldShellCfg = props.cfg.shell_injection ?? {};
                                    props.onChange({ ...props.cfg, shell_injection: { ...oldShellCfg, subprocess: valueList } })
                                }} />
                        </Form.Item>
                        <Form.Item label="参数shell">
                            <ArrayInput valueList={props.cfg.shell_injection?.shell ?? []}
                                onChange={valueList => {
                                    const oldShellCfg = props.cfg.shell_injection ?? {};
                                    props.onChange({ ...props.cfg, shell_injection: { ...oldShellCfg, shell: valueList } })
                                }} />
                        </Form.Item>
                        <Form.Item label="参数no_shell">
                            <ArrayInput valueList={props.cfg.shell_injection?.no_shell ?? []}
                                onChange={valueList => {
                                    const oldShellCfg = props.cfg.shell_injection ?? {};
                                    props.onChange({ ...props.cfg, shell_injection: { ...oldShellCfg, no_shell: valueList } })
                                }} />
                        </Form.Item>
                    </Form>
                </Card>
            )}
        </>
    );
}